package cn.wk.libs4a.view.adview;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView.ScaleType;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.view.WKWebAct;

public class ADPaperAdapter extends PagerAdapter {

	private ArrayList<ADViewBean> mdata;
	Context mContext;
	private boolean clickable = true;

	public WKApplication app() {
		return (WKApplication) mContext.getApplicationContext();
	}

	public ADPaperAdapter(Context context) {
		mContext = context;
	}

	public ADPaperAdapter(Context context, boolean clickable) {
		this.mContext = context;
		this.clickable = clickable;
	}

	public void setData(ArrayList<ADViewBean> data) {
		this.mdata = data;
		System.out.println("mdata   -  " + mdata);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (mdata == null)
			return 0;
		return mdata.size();
	}

	private int position;

	@Override
	public View instantiateItem(ViewGroup container, int _position) {

		// View convertView = View.inflate(mContext, R.layout.home_gallery_item,
		// null);
		// GalleryImageView imageView = (GalleryImageView) convertView
		// .findViewById(R.id.item_image);
		GalleryImageView view = new GalleryImageView(mContext);
		view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		view.setScaleType(ScaleType.CENTER_CROP);

		position = _position;
		String url = mdata.get(_position).target;
		if (url == null)
			url = mdata.get(_position).url;
		String item = mdata.get(_position).img;
		if (item != null) {
			// imageView.setDefault(R.drawable.zhanwei_2);
			view.setUrl(item);
			view.reload();
			view.setTag(position);
		}
		view.setTag(url);
		if (clickable)
			view.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					String url = (String) v.getTag();
					Intent i = new Intent(mContext, WKWebAct.class);
					i.putExtra("url", url);
					i.putExtra("title", "广告页");
					mContext.startActivity(i);
				}
			});
		container.addView(view);
		return view;

	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		try {
			if (((GalleryImageView) object) != null) {
				((GalleryImageView) object).recycle(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		container.removeView((View) object);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	class BtnClick implements OnClickListener {
		String link;
		String[] keywords;
		String lastKey;
		String lastSecond;
		int length;

		public BtnClick(String link) {
			this.link = link;
			keywords = link.split("/");
			length = keywords.length;
			if (length != 0) {
				lastKey = keywords[length - 1];
				lastSecond = keywords[length - 2];
			}
		}

		@Override
		public void onClick(View arg0) {
		}

	}
}
